package com.wg.admin.runner;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.wg.common.config.FastConfig;
import com.wg.system.service.SysConfigService;
import com.wg.system.service.SysDictTypeService;
import com.wg.system.service.SysFileConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author wg
 * @date 2022-03-05
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StartedUpRunner implements ApplicationRunner {

    private final ConfigurableApplicationContext context;
    private final Environment environment;
    private final FastConfig fastConfig;
    private final SysConfigService configService;
    private final SysDictTypeService dictTypeService;
    private final SysFileConfigService fileConfigService;

    @Override
    public void run(ApplicationArguments args) {

        fileConfigService.init();
        log.info("初始化 file 配置成功");

        if (!fastConfig.isCacheLazy()) {
            configService.loadingConfigCache();
            log.info("加载参数缓存数据成功");
            dictTypeService.loadingDictCache();
            log.info("加载字典缓存数据成功");
        }

        if (context.isActive()) {
            String ip;
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                ip = "127.0.0.1";
            }
            String port = environment.getProperty("server.port");
            String path = environment.getProperty("server.servlet.context-path");
            String banner = "------------------------------------------------------------------------------------------------\n" +
                    "\t服务启动成功，时间：" + DateUtil.now() + "\n" +
                    "\t服务名称：" + "瓜瓜快速开发系统" + "\n" +
                    "\t地址Local：http://127.0.0.1:" + port + (StrUtil.isBlank(path) ? "" : path + "") + "\n" +
                    "\t地址Extrol：http://" + ip + ":" + port + (StrUtil.isBlank(path) ? "" : path + "") + "\n" +
                    "\t接口地址：http://127.0.0.1:" + port + (StrUtil.isBlank(path) ? "" : path + "/doc.html") + "\n" +
                    "------------------------------------------------------------------------------------------------";
            System.out.println(banner);
        }
    }
}
